<?php

namespace addons\qingdongams\controller;


use addons\qingdongams\model\ExamineRecord;
use addons\qingdongams\model\File;
use addons\qingdongams\model\Invoice as InvoiceModel;
use addons\qingdongams\model\Message;
use addons\qingdongams\model\Staff;
use think\Db;
use think\Exception;

/**
 * 发票接口
 */
class Invoice extends StaffApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = [];


    //添加
    public function addInvoice()
    {
        $params = $this->request->post();

        // 表单验证
        if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
            $this->error($result);
        }

        Db::startTrans();
        try {
            $lastId = InvoiceModel::createInvoice($params);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success('添加发票成功',['id'=>$lastId]);
        }
    }

    //编辑
    public function editInvoice()
    {
        $params = $this->request->post();

        // 表单验证
        if (($result = $this->qingdongamsValidate($params, get_class(),'create')) !== true) {
            $this->error($result);
        }
        Db::startTrans();
        try {
            $result = InvoiceModel::updateInvoice($params);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success('编辑发票成功');
        }
    }



    //获取列表
    public function getList()
    {
        $limit = input("limit/d", 10);
        $customer_id = input('customer_id');
        $contract_id = input('contract_id');
        $params = $this->request->post();

        if (isset($params['createtime']) && $params['createtime']) {//
            $createtime = $params['createtime'];
            $where['createtime'] = ['between', setTimes($createtime,'time')];
        }

        $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
        if (isset($params['type']) && $params['type']) {//分类
            if ($params['type'] == 1) {//我的创建
                $where['owner_staff_id'] = $this->auth->id;
            } elseif ($params['type'] == 2) {//下属创建
                $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
            }
        }

        if ($customer_id) {
            $where['customer_id'] = $customer_id;
        }
        if ($contract_id) {
            $where['contract_id'] = $contract_id;
        }
        $records = InvoiceModel::where($where)->with(['ownerStaff','customer','contract'])->order('id desc')->paginate($limit);
        $this->success('请求成功', $records);
    }


    //获取详情
    public function getDetail()
    {
        $id = input('id', '', 'intval');
        $invoice = InvoiceModel::where(['id' => $id])
            ->with(['ownerStaff', 'customer','contract'])->find();
        if (empty($invoice)) {
            $this->error('发票不存在');
        }
        if ($invoice['check_status'] == 0 || $invoice['check_status'] == 1) {
            $invoice['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::INVOICE_TYPE, $id);
        } else {
            $invoice['is_examine'] = 0;
        }
        //标记通知已读
        Message::setRead(Message::INVOICE_TYPE, $id, $this->auth->id);

        $this->success('请求成功', $invoice);
    }


    //获取编号
    public function getNumber()
    {
        $this->success('请求成功', ['number' => InvoiceModel::getNumber()]);
    }
    //标记为开票
    public function startInvoice(){
        $params = $this->request->post();
        if(!$params['id']){
            $this->error('参数不正确');
        }
        if(!$params['invoice_time']){
            $this->error('实际开票日期不能为空');
        }
        Db::startTrans();
        try {
            if($params['image']){
                $params['image'] = File::where(['id'=>$params['image']])->value('file_path');
            }
            $result = InvoiceModel::updateInfo($params);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success('编辑发票成功');
        }
    }

}
